<template>
  <div class="login-container">
    <div class="login-box">
      <!-- 头像区域 -->
      <div class="avatar-box">
        <img class="avatar" src="../assets/logo.png" alt="logo" />
      </div>
      <!-- 表单区域 -->
      <div class="form-login">
        <!-- 登录名称 -->
        <div class="form-group">
          <label for="username">登录名称:</label>
          <input
            type="text"
            class="form-control"
            id="username"
            placeholder="请输入登录名称"
            autocomplete="off"
            v-model.trim="username"
          />
        </div>
        <!-- 登录密码 -->
        <div class="form-group">
          <label for="password">登录密码:</label>
          <input
            type="password"
            class="form-control"
            id="password"
            placeholder="请输入登录密码"
            v-model.trim="password"
          />
        </div>
        <!-- 登录按钮 -->
        <div class="form-group">
          <button type="button" class="btn" @click="onLogin">登录</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const router = useRouter()
const username = ref('')
const password = ref('')

// 登录事件处理
const onLogin = () => {
  if (username.value === 'admin' && password.value === '123456') {
    // 登录成功：存储token并跳转首页
    localStorage.setItem('token', 'Bearer xxx')
    router.push('/home')
  } else {
    // 登录失败：提示并清除token
    alert('用户名或密码输入错误')
    localStorage.removeItem('token')
  }
}
</script>

<style scoped lang="less">
.login-container {
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #f5f5f5;

  .login-box {
    width: 400px;
    padding: 30px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);

    .avatar-box {
      display: flex;
      justify-content: center;
      margin-bottom: 20px;

      .avatar {
        width: 80px;
        height: 80px;
        border-radius: 50%;
      }
    }

    .form-login {
      .form-group {
        margin-bottom: 20px;
        display: flex;
        flex-direction: column;

        label {
          font-size: 14px;
          color: #333;
          margin-bottom: 8px;
        }

        .form-control {
          padding: 10px;
          border: 1px solid #ddd;
          border-radius: 4px;
          font-size: 14px;
          outline: none;
          transition: border-color 0.3s;

          &:focus {
            border-color: #409eff;
          }
        }

        .btn {
          padding: 12px;
          background: #409eff;
          color: #fff;
          border: none;
          border-radius: 4px;
          font-size: 16px;
          cursor: pointer;
          transition: background 0.3s;

          &:hover {
            background: #3a8ee6;
          }
        }
      }
    }
  }
}
</style>